package cn.e4j.modular.crm.service.impl;

import cn.e4j.core.dto.impl.Dto;
import cn.e4j.core.shiro.ShiroKit;
import cn.e4j.core.shiro.ShiroUser;
import cn.e4j.core.util.DateUtils;
import cn.e4j.core.util.LangUtils;
import cn.e4j.modular.crm.entity.GwaqT;
import cn.e4j.modular.crm.entity.GwaqlT;
import cn.e4j.modular.crm.mapper.GwaqTMapper;
import cn.e4j.modular.crm.service.GwaqTService;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  客户等级 服务实现类
 * </p>
 *
 * @author miaoyl
 * @since 2019-01-10
 */
@Service
public class GwaqTServiceImpl extends ServiceImpl<GwaqTMapper, GwaqT> implements GwaqTService {

    @Transactional(readOnly = true)
    public List<Map<String, Object>> getGwaqlist(Page<Object> page, Dto dto) {

        List<Map<String, Object>> gwaqDtos = this.baseMapper.listGwaqPage(page, dto);

        return gwaqDtos;
    }

    @Transactional(readOnly = true)
    public GwaqT getByGwaq001(String ent,String gwaq001) {
        GwaqT gwaq = this.baseMapper.getByGwaq001(ent, gwaq001);
        return gwaq;
    }

    @Transactional
    @Override
    public void add(Dto dto) {
        ShiroUser shiroUser = ShiroKit.getUser();
        GwaqT gwaq = new GwaqT();
        gwaq.setGWAQ001(dto.getString("GWAQ001"));
        gwaq.setGWAQ002(" ");//备用字段
        gwaq.setGWAQ003(" ");//备用字段
        gwaq.setGWAQENT(shiroUser.getDBC());
        gwaq.setGWAQACTI(dto.getString("GWAQACTI"));
        gwaq.setGWAQUSER(shiroUser.getACCOUNT());
        gwaq.setGWAQGRUP(shiroUser.getORG_ID());
        gwaq.setGWAQDATE(LocalDate.now());
        gwaq.setGWAQTIME(DateUtils.formatDate(new Date(), "HH:mm:ss"));
        gwaq.setGWAQORIG(shiroUser.getORG_ID());
        gwaq.setGWAQORIU(shiroUser.getACCOUNT());
        GwaqlT gwaql = new GwaqlT();
        gwaql.setGWAQLENT(shiroUser.getDBC());
        gwaql.setGWAQL001(dto.getString("GWAQ001"));
        gwaql.setGWAQL002(LangUtils.getLang(shiroUser.getLANG()));
        gwaql.setGWAQL003(dto.getString("GWAQL003"));
        gwaql.setGWAQL004(dto.getString("GWAQL004"));
        gwaq.insert();
        gwaql.insert();
    }

    @Transactional
    @Override
    public void edit(Dto dto) {
        ShiroUser shiroUser = ShiroKit.getUser();
        GwaqT gwaq = new GwaqT();
        gwaq.setGWAQACTI(dto.getString("GWAGACTI"));
        gwaq.setGWAQMODU(shiroUser.getACCOUNT());
        UpdateWrapper<GwaqT> gwaqTUpdateWrapper = new UpdateWrapper<>();
        gwaqTUpdateWrapper.eq("GWAQENT",shiroUser.getDBC());
        gwaqTUpdateWrapper.eq("GWAQ001",dto.getString("GWAQ001"));

        GwaqlT gwaql = new GwaqlT();
        gwaql.setGWAQL003(dto.getString("GWAQL003"));
        gwaql.setGWAQL004(dto.getString("GWAQL004"));
        UpdateWrapper<GwaqlT> gwaqlTUpdateWrapper = new UpdateWrapper<>();
        gwaqlTUpdateWrapper.eq("GWAQLENT",shiroUser.getDBC());
        gwaqlTUpdateWrapper.eq("GWAQL001",dto.getString("GWAQ001"));

        gwaq.update(gwaqTUpdateWrapper);
        gwaql.update(gwaqlTUpdateWrapper);
    }


    @Transactional
    @Override
    public void delete(Dto dto) {
        ShiroUser shiroUser = ShiroKit.getUser();
        GwaqT gwaq = new GwaqT();
        UpdateWrapper<GwaqT> gwaqTUpdateWrapper = new UpdateWrapper<>();
        gwaqTUpdateWrapper.eq("GWAQENT",shiroUser.getDBC());
        gwaqTUpdateWrapper.eq("GWAQ001",dto.getString("GWAQ001"));

        GwaqlT gwaql = new GwaqlT();
        UpdateWrapper<GwaqlT> gwaqlTUpdateWrapper = new UpdateWrapper<>();
        gwaqlTUpdateWrapper.eq("GWAQLENT",shiroUser.getDBC());
        gwaqlTUpdateWrapper.eq("GWAQL001",dto.getString("GWAQ001"));

        gwaq.delete(gwaqTUpdateWrapper);
        gwaql.delete(gwaqlTUpdateWrapper);
    }

    @Transactional
    @Override
    public void delDataBatch(List<GwaqT> gwaqTList) {
        ShiroUser shiroUser = ShiroKit.getUser();
        for(GwaqT gwaqT : gwaqTList){
            UpdateWrapper<GwaqT> gwaqTUpdateWrapper = new UpdateWrapper<>();
            gwaqTUpdateWrapper.eq("GWAQENT",shiroUser.getDBC());
            gwaqTUpdateWrapper.eq("GWAQ001",gwaqT.getGWAQ001());

            GwaqlT gwaql = new GwaqlT();
            UpdateWrapper<GwaqlT> gwaqlTUpdateWrapper = new UpdateWrapper<>();
            gwaqlTUpdateWrapper.eq("GWAQLENT",shiroUser.getDBC());
            gwaqlTUpdateWrapper.eq("GWAQL001",gwaqT.getGWAQ001());

            gwaqT.delete(gwaqTUpdateWrapper);
            gwaql.delete(gwaqlTUpdateWrapper);
        }
    }
}
